﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using XingFuStudio.Pioneer.Net;
using XingFuStudio.Pioneer.Entity;
using XingFuStudio.Pioneer.IO;
using XingFuStudio.Pioneer.Applications.City.Entity;
using XingFuStudio.Pioneer.Applications.City.Config;
using FluorineFx.IO;

namespace XingFuStudio.Pioneer.Applications.City.Operation {
    /// <summary>
    /// 好友相关操作
    /// </summary>
    public class Friend : OPBase{
        /// <summary>
        /// 调用方法的目标类
        /// </summary>
        //private readonly string Target = "CCityAction.callfunc";

        //初始化共公操作对象
        OPCommon opCommon = null;

        /// <summary>
        /// 创建业务对象
        /// </summary>
        /// <param name="httpManager"></param>
        /// <param name="account"></param>
        /// <param name="city"></param>
        /// <param name="config"></param>
        public Friend(HttpManager httpManager, AccountInfo account, CityInfo city, UserConfigSection config)
            : base(httpManager, account, city, config) {

            //初始化公共操作对象
            this.opCommon = new OPCommon(this.httpManager, this.account, this.city);
        }

        /// <summary>
        /// 重写操作方法
        /// </summary>
        public override void Operate() {
            base.Operate();

            string message = String.Empty;

            //获取好友列表
            List<CityFriendInfo> cityFriends = this.GetFriends();

            cityFriends = new List<CityFriendInfo>();
            cityFriends.Add(new CityFriendInfo(){
                UserName = "王海群",
                UserId = "6123357",
                IsInstall = true
            });
            cityFriends.Add(new CityFriendInfo() {
                UserName = "韩丽",
                UserId = "4720077",
                IsInstall = true
            });
            cityFriends.Add(new CityFriendInfo() {
                UserName = "赵敏",
                UserId = "42741107",
                IsInstall = true
            });

            //获取所有安装了游戏的好友
            var fes = cityFriends.Where(s => s.IsInstall);

            //遍历好友
            foreach (CityFriendInfo friend in fes) {
                try {
                    //打印日志
                    this.DoPrintLog(true, String.Format("访问{0}的城市", friend.UserName));

                    bool result = this.ViewFriendCity(friend, out message);
                    if (result) {
                        //打印日志
                        this.DoPrintLog(true, message);

                        result = this.FestivalTree(friend, out message);
                        if (result) {
                            //打印日志
                            this.DoPrintLog(true, message);
                        }
                    }
                } catch (Exception ex) {
                    this.DoError(ex);
                }
            }
        }

        /// <summary>
        /// 获取商品列表
        /// </summary>
        /// <param name="shopType">商品类型</param>
        /// <returns></returns>
        public List<CityFriendInfo> GetFriends() {
            //获取AMF数据
            AMFMessage message = this.GetAMFMessage("CCityFriend.callfunc", "getList", "all", "", "", "");

            //解析数据
            Dictionary<string, object> content = (Dictionary<string, object>)message.Bodies[0].Content;
            Object[] data = (Object[])content["data"];

            List<CityFriendInfo> list = new List<CityFriendInfo>();

            //遍历数据
            foreach (Object[] items in data) {
                //创建商品信息实例对象
                CityFriendInfo friend = new CityFriendInfo();

                //初始化商品信息
                friend.UserName = (string)items[1];
                friend.UserId = (string)items[7];
                friend.IsInstall = ((string)items[6]) == "uninstall" ? false : true;

                //添加到列表
                list.Add(friend);
            }

            //返回商品列表
            return list;
        }

        /// <summary>
        /// 给好友送愿望元素
        /// </summary>
        /// <param name="frinedId">好友ID</param>
        /// <param name="elementId">好友愿望元素ID</param>
        /// <param name="resultMessage">输出参数，用来返回结果信息</param>
        /// <returns></returns>
        public bool SendWishElement(string frinedId, int elementId, out string resultMessage) {
            //获取AMF数据
            AMFMessage message = this.GetAMFMessage("CCityGiftWish.callfunc", "sendWishElements2Friend",
                frinedId, elementId);

            //解析数据
            Dictionary<string, object> content = (Dictionary<string, object>)message.Bodies[0].Content;
            Dictionary<string, object> data = (Dictionary<string, object>)content["data"];
            //获取收获是否成功的状态
            bool result = (string)data["result"] == "succ";
            resultMessage = ((string)data["msg"]).NoHtml();

            return result;
        }

        /// <summary>
        /// 送钱
        /// </summary>
        /// <param name="frinedId">好友ID</param>
        /// <param name="resultMessage">输出参数，用来返回结果信息</param>
        /// <returns></returns>
        public bool SendMoney(string frinedId, out string resultMessage) {
            //获取AMF数据
            AMFMessage message = this.GetAMFMessage("CCitySendMoney.callfunc", "sendmoney",
                frinedId, "normal", 5000, "sugguest");

            //解析数据
            Dictionary<string, object> content = (Dictionary<string, object>)message.Bodies[0].Content;
            Dictionary<string, object> data = (Dictionary<string, object>)content["data"];
            //获取收获是否成功的状态
            bool result = (string)data["result"] == "succ";
            resultMessage = ((string)data["word"]).NoHtml();

            return result;
        }

        /// <summary>
        /// 给好友送礼物
        /// </summary>
        /// <param name="friendIds">要送礼物的好友ID列表</param>
        /// <param name="elementId">礼物元素ID</param>
        /// <param name="resultMessage">输出参数，用来返回结果信息</param>
        /// <returns></returns>
        public bool SendGift(List<string> friendIds, int elementId, out string resultMessage) {
            //声明参数字典
            List<object> friends = new List<object>();
            friends.AddRange(friendIds);

            //获取AMF数据
            AMFMessage message = this.GetAMFMessage("CCityGiftSend.callfunc", "sendGift2Friends",
                friends, (double)elementId, "");

            //解析数据
            Dictionary<string, object> content = (Dictionary<string, object>)message.Bodies[0].Content;
            Dictionary<string, object> data = (Dictionary<string, object>)content["data"];

            //获取收获是否成功的状态
            bool result = (string)data["result"] == "succ";
            resultMessage = ((string)data["msg"]).NoHtml();

            return result;
        }

        /// <summary>
        /// 获取是否收到礼物
        /// </summary>
        /// <returns></returns>
        public bool GetHasReceiveGift() {
            //获取AMF数据
            AMFMessage message = this.GetAMFMessage("CCityGiftRecv.callfunc", "getRecvList");

            //解析数据
            Dictionary<string, object> content = (Dictionary<string, object>)message.Bodies[0].Content;
            object[] data = (object[])content["data"];

            return data.Length > 0;
        }

        /// <summary>
        /// 接收全部礼物
        /// </summary>
        /// <param name="resultMessage">输出参数，用来返回结果信息</param>
        /// <returns></returns>
        public bool ReceiveAllGift(out string resultMessage) {
            //获取AMF数据
            AMFMessage message = this.GetAMFMessage("CCityGiftRecv.callfunc", "recvAllGift");

            //解析数据
            Dictionary<string, object> content = (Dictionary<string, object>)message.Bodies[0].Content;
            Dictionary<string, object> data = (Dictionary<string, object>)content["data"];

            //获取收获是否成功的状态
            bool result = (string)data["result"] == "succ";
            resultMessage = ((string)data["msg"]).NoHtml();

            return result;
        }

        /// <summary>
        /// 访问好友家
        /// </summary>
        /// <param name="friend"></param>
        private bool ViewFriendCity(CityFriendInfo friend, out string resultMessage) {
            //访问好友家
            CityInfo friendCity = opCommon.GetCityInfo(friend);

            //判断是否已经访问过了
            if (friendCity.VisitGet != null) {
                int exp = Convert.ToInt32((string)friendCity.VisitGet["xp"]);
                int cash = Convert.ToInt32((string)friendCity.VisitGet["cash"]);
                int energy = Convert.ToInt32((string)friendCity.VisitGet["energy"]);

                resultMessage = String.Format("{0}经验增加{1} 现金增加{2} 体力增加{3}",
                    (string)friendCity.VisitGet["msg"], exp, cash, energy);

                city.Cash += cash;
                city.Exp += exp;
                city.Energy += energy;

                return true;
            }

            resultMessage = String.Empty;

            return false;
        }

        /// <summary>
        /// 访问好友爱情树
        /// </summary>
        /// <param name="friend"></param>
        private bool FestivalTree(CityFriendInfo friend, out string resultMessage) {
            //获取AMF数据
            AMFMessage message = this.GetAMFMessage("CCityFestivalActivity.callfunc", "search", friend.UserId);

            //解析数据
            Dictionary<string, object> content = (Dictionary<string, object>)message.Bodies[0].Content;
            Dictionary<string, object> data = (Dictionary<string, object>)content["data"];

            bool result = ((string)data["result"]) == "succ";

            if (result) {
                Dictionary<string, object> tree = (Dictionary<string, object>)data["treeInfo"];
                resultMessage = ((string)tree["msg"]).NoHtml();

                return true;
            }

            resultMessage = String.Empty;

            return false;
        }
    }
}
